package com.landinfo.service.impl;

import com.landinfo.service.EquipmentManagementService;
import com.landinfo.domain.EquipmentManagement;
import com.landinfo.repository.EquipmentManagementRepository;
import com.landinfo.service.dto.EquipmentManagementDTO;
import com.landinfo.service.mapper.EquipmentManagementMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * Service Implementation for managing EquipmentManagement.
 */
@Service
@Transactional
public class EquipmentManagementServiceImpl implements EquipmentManagementService{

    private final Logger log = LoggerFactory.getLogger(EquipmentManagementServiceImpl.class);

    private final EquipmentManagementRepository equipmentManagementRepository;

    private final EquipmentManagementMapper equipmentManagementMapper;

    public EquipmentManagementServiceImpl(EquipmentManagementRepository equipmentManagementRepository, EquipmentManagementMapper equipmentManagementMapper) {
        this.equipmentManagementRepository = equipmentManagementRepository;
        this.equipmentManagementMapper = equipmentManagementMapper;
    }

    /**
     * Save a equipmentManagement.
     *
     * @param equipmentManagementDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public EquipmentManagementDTO save(EquipmentManagementDTO equipmentManagementDTO) {
        log.debug("Request to save EquipmentManagement : {}", equipmentManagementDTO);
        EquipmentManagement equipmentManagement = equipmentManagementMapper.toEntity(equipmentManagementDTO);
        equipmentManagement = equipmentManagementRepository.save(equipmentManagement);
        return equipmentManagementMapper.toDto(equipmentManagement);
    }

    /**
     *  Get all the equipmentManagements.
     *
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<EquipmentManagementDTO> findAll(Pageable pageable) {
        log.debug("Request to get all EquipmentManagements");
        Pageable pageRequest= new PageRequest(pageable.getPageNumber(), pageable.getPageSize(), new Sort(Sort.Direction.DESC, new String[] { "createDate" }));

        return equipmentManagementRepository.findAll(pageRequest).map(equipmentManagementMapper::toDto);
    }

    @Override
    public Iterable<EquipmentManagement> findAll() {
        return equipmentManagementRepository.findAll();
    }

    /**
     *  Get one equipmentManagement by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public EquipmentManagementDTO findOne(Long id) {
        log.debug("Request to get EquipmentManagement : {}", id);
        EquipmentManagement equipmentManagement = equipmentManagementRepository.findById(id).get();
        return equipmentManagementMapper.toDto(equipmentManagement);
    }

    @Override
    public EquipmentManagement findByImei(String imei) {
        return equipmentManagementRepository.findByImei(imei);
    }

    /**
     *  Delete the  equipmentManagement by id.
     *
     *  @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete EquipmentManagement : {}", id);
        equipmentManagementRepository.deleteById(id);
    }
}
